package com.gqj.elec.service.impl;

import java.util.Date;
import java.util.List;

import com.gqj.common.exception.ServiceException;
import com.gqj.common.utils.DateUtils;
import com.gqj.common.utils.uuid.HiultraUtil;
import com.gqj.elec.domain.ElecMaterial;
import com.gqj.elec.domain.ElecMaterialIndetail;
import com.gqj.elec.mapper.ElecMaterialIndetailMapper;
import com.gqj.elec.mapper.ElecMaterialMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gqj.elec.mapper.ElecMaterialIninfoMapper;
import com.gqj.elec.domain.ElecMaterialIninfo;
import com.gqj.elec.service.IElecMaterialIninfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物资入库Service业务层处理
 *
 * @author lxq
 * @date 2023-12-17
 */
@Service
public class ElecMaterialIninfoServiceImpl implements IElecMaterialIninfoService {
    @Autowired
    private ElecMaterialIninfoMapper elecMaterialIninfoMapper;
    @Autowired
    private ElecMaterialIndetailMapper elecMaterialIndetailMapper;
    @Autowired
    private ElecMaterialMapper elecMaterialMapper;

    /**
     * 查询物资入库
     *
     * @param inId 物资入库主键
     * @return 物资入库
     */
    @Override
    public ElecMaterialIninfo selectElecMaterialIninfoByInId(Long inId) {
        ElecMaterialIninfo elecMaterialIninfo = elecMaterialIninfoMapper.selectElecMaterialIninfoByInId(inId);

        ElecMaterialIndetail elecMaterialIndetail = new ElecMaterialIndetail();
        elecMaterialIndetail.setInId(inId);

        List<ElecMaterialIndetail> elecMaterialList = elecMaterialIndetailMapper.selectElecMaterialIndetailList(elecMaterialIndetail);
        elecMaterialIninfo.setElecMaterialList(elecMaterialList);
        return elecMaterialIninfo;
    }

    /**
     * 查询物资入库列表
     *
     * @param elecMaterialIninfo 物资入库
     * @return 物资入库
     */
    @Override
    public List<ElecMaterialIninfo> selectElecMaterialIninfoList(ElecMaterialIninfo elecMaterialIninfo) {
        return elecMaterialIninfoMapper.selectElecMaterialIninfoList(elecMaterialIninfo);
    }

    /**
     * 新增物资入库
     *
     * @param elecMaterialIninfo 物资入库
     * @return 结果
     */
    @Override
    @Transactional
    public int insertElecMaterialIninfo(ElecMaterialIninfo elecMaterialIninfo) {

        Date nowDate = DateUtils.getNowDate();
        elecMaterialIninfo.setBatchNumber("RK-" + HiultraUtil.getOrderNo());
        elecMaterialIninfo.setInStatus("1");
        elecMaterialIninfo.setInType("1");
        elecMaterialIninfo.setCreateTime(DateUtils.getNowDate());
        int i = elecMaterialIninfoMapper.insertElecMaterialIninfo(elecMaterialIninfo);

        List<ElecMaterialIndetail> elecMaterialList = elecMaterialIninfo.getElecMaterialList();
        for (ElecMaterialIndetail material : elecMaterialList) {
            ElecMaterialIndetail elecMaterialIndetail = new ElecMaterialIndetail();
            elecMaterialIndetail.setInId(elecMaterialIninfo.getInId());
            elecMaterialIndetail.setMaterialId(material.getMaterialId());
            elecMaterialIndetail.setInNumber(material.getInNumber());
            elecMaterialIndetail.setCreateBy(elecMaterialIninfo.getCreateBy());
            elecMaterialIndetail.setCreateTime(nowDate);
            if (elecMaterialIninfo.getMaterialCategory().equals("3")) {
                elecMaterialIndetail.setIsIn("1");
            } else {
                elecMaterialIndetail.setIsIn("0");
            }
            elecMaterialIndetailMapper.insertElecMaterialIndetail(elecMaterialIndetail);

            ElecMaterial temp = new ElecMaterial();
            temp.setMaterialId(material.getMaterialId());

            if (elecMaterialIninfo.getMaterialCategory().equals("3")) {
                if(null == material.getInNumber() || material.getInNumber() <= 0){
                    throw new ServiceException(material.getMaterialName() + "的入库数量小于等于0或为空，无法进行出库操作！");
                }

                Integer numberResult = 0;
                ElecMaterial elecMaterial = elecMaterialMapper.selectElecMaterialByMaterialId(material.getMaterialId());
                Integer number = elecMaterial.getNumber();
                if (null != number) {
                    numberResult = number + material.getInNumber();
                }
                temp.setNumber(numberResult);
            } else {
                temp.setMaterialStatus("11");
            }
            elecMaterialMapper.updateElecMaterial(temp);

        }
        return i;
    }

    /**
     * 修改物资入库
     *
     * @param elecMaterialIninfo 物资入库
     * @return 结果
     */
    @Override
    @Transactional
    public int updateElecMaterialIninfo(ElecMaterialIninfo elecMaterialIninfo) {
        elecMaterialIninfo.setUpdateTime(DateUtils.getNowDate());
        int i = elecMaterialIninfoMapper.updateElecMaterialIninfo(elecMaterialIninfo);

        ElecMaterialIninfo entity = elecMaterialIninfoMapper.selectElecMaterialIninfoByInId(elecMaterialIninfo.getInId());

        ElecMaterialIndetail elecMaterialIndetail = new ElecMaterialIndetail();
        elecMaterialIndetail.setInId(entity.getInId());
        List<ElecMaterialIndetail> elecMaterialList = elecMaterialIndetailMapper.selectElecMaterialIndetailList(elecMaterialIndetail);
        for (ElecMaterialIndetail emi : elecMaterialList) {

            if (null == emi.getSparePartType() || "".equals(emi.getSparePartType()) || "1".equals(emi.getSparePartType())) {
                ElecMaterial temp = new ElecMaterial();
                temp.setMaterialId(emi.getMaterialId());
                temp.setMaterialStatus("0");
                elecMaterialMapper.updateElecMaterial(temp);
            }
        }
        elecMaterialIndetailMapper.deleteElecMaterialIndetailByInId(elecMaterialIninfo.getInId());

        List<ElecMaterialIndetail> elecMaterialListT = elecMaterialIninfo.getElecMaterialList();
        for (ElecMaterialIndetail material : elecMaterialListT) {
            ElecMaterialIndetail elecMaterialIndetailT = new ElecMaterialIndetail();
            elecMaterialIndetailT.setInId(elecMaterialIninfo.getInId());
            elecMaterialIndetailT.setMaterialId(material.getMaterialId());
            elecMaterialIndetailT.setUpdateBy(elecMaterialIninfo.getUpdateBy());
            elecMaterialIndetailT.setUpdateTime(DateUtils.getNowDate());
            elecMaterialIndetail.setIsIn("0");
            elecMaterialIndetailMapper.insertElecMaterialIndetail(elecMaterialIndetailT);

            if (null == material.getSparePartType() || "".equals(material.getSparePartType()) || "1".equals(material.getSparePartType())) {
                ElecMaterial temp = new ElecMaterial();
                temp.setMaterialId(material.getMaterialId());
                temp.setMaterialStatus("11");
                elecMaterialMapper.updateElecMaterial(temp);
            }
        }


        return i;
    }

    /**
     * 批量删除物资入库
     *
     * @param inIds 需要删除的物资入库主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteElecMaterialIninfoByInIds(Long[] inIds) {
        for (Long inId : inIds) {
            ElecMaterialIninfo entity = elecMaterialIninfoMapper.selectElecMaterialIninfoByInId(inId);

            ElecMaterialIndetail elecMaterialIndetail = new ElecMaterialIndetail();
            elecMaterialIndetail.setInId(entity.getInId());
            List<ElecMaterialIndetail> elecMaterialList = elecMaterialIndetailMapper.selectElecMaterialIndetailList(elecMaterialIndetail);
            for (ElecMaterialIndetail emi : elecMaterialList) {
                if (null == emi.getSparePartType() || "".equals(emi.getSparePartType()) || "1".equals(emi.getSparePartType())) {
                    ElecMaterial temp = new ElecMaterial();
                    temp.setMaterialId(emi.getMaterialId());
                    temp.setMaterialStatus("0");
                    elecMaterialMapper.updateElecMaterial(temp);
                }
            }
        }

        return elecMaterialIninfoMapper.deleteElecMaterialIninfoByInIds(inIds);
    }

    /**
     * 删除物资入库信息
     *
     * @param inId 物资入库主键
     * @return 结果
     */
    @Override
    public int deleteElecMaterialIninfoByInId(Long inId) {
        return elecMaterialIninfoMapper.deleteElecMaterialIninfoByInId(inId);
    }
}
